/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#include "ed_local.h"


static const char *	ed_editorNames[] = {
	"none",
	"light",
	"sound",
	"effect",
	"reverb"
};

static editor_t		ed_editorActive;

static bool			(*ed_mouseEventCallback)(void);
static void			(*ed_refreshCallback)(void);


/*
 ==================
 ED_LaunchEditor
 ==================
*/
bool ED_LaunchEditor (editor_t editor, bool (*mouseEvent)(void), void (*refresh)(void)){

	if (ed_editorActive != EDITOR_NONE){
		Com_Printf("You must close the %s editor before launching the %s editor\n", ed_editorNames[ed_editorActive], ed_editorNames[editor]);
		return false;
	}

	if (!CVar_AllowCheats()){
		Com_Printf("You must enable cheats to use the %s editor\n", ed_editorNames[editor]);
		return false;
	}

	if (cls.state != CS_ACTIVE){
		Com_Printf("You must be in a map to use the %s editor\n", ed_editorNames[editor]);
		return false;
	}

	if (cls.glConfig.isFullscreen){
		Com_Printf("You must run the game in windowed mode to use the %s editor\n", ed_editorNames[editor]);
		return false;
	}

	// Create the editor window
	switch (editor){
	case EDITOR_LIGHT:
		if (!ED_CreateLightEditorWindow())
			return false;

		break;
	case EDITOR_SOUND:
		// TODO!!!
		break;
	case EDITOR_EFFECT:
		// TODO!!!
		break;
	case EDITOR_REVERB:
		if (!ED_CreateReverbEditorWindow())
			return false;

		break;
	default:
		Com_Error(true, "ED_LaunchEditor: bad editor (%i)", editor);
	}

	// Set edit-mode on
	Key_SetEditMode(true);

	// Save the editor state
	ed_editorActive = editor;

	ed_mouseEventCallback = mouseEvent;
	ed_refreshCallback = refresh;

	return true;
}

/*
 ==================
 ED_CloseEditor
 ==================
*/
void ED_CloseEditor (void){

	if (ed_editorActive == EDITOR_NONE)
		return;

	// Destroy the editor window
	switch (ed_editorActive){
	case EDITOR_LIGHT:
		ED_DestroyLightEditorWindow();
		break;
	case EDITOR_SOUND:
		// TODO!!!
		break;
	case EDITOR_EFFECT:
		// TODO!!!
		break;
	case EDITOR_REVERB:
		ED_DestroyReverbEditorWindow();
		break;
	default:
		Com_Error(true, "ED_CloseEditor: bad editor (%i)", ed_editorActive);
	}

	// Set edit-mode off
	Key_SetEditMode(false);

	// Clear the editor state
	ed_editorActive = EDITOR_NONE;

	ed_mouseEventCallback = NULL;
	ed_refreshCallback = NULL;
}

/*
 ==================
 ED_MouseEvent
 ==================
*/
bool ED_MouseEvent (void){

	if (ed_editorActive == EDITOR_NONE)
		return false;

	// Perform a callback if possible
	if (!ed_mouseEventCallback)
		return false;

	return ed_mouseEventCallback();
}

/*
 ==================
 ED_Refresh
 ==================
*/
void ED_Refresh (void){

	if (ed_editorActive == EDITOR_NONE)
		return;

	// Perform a callback if possible
	if (!ed_refreshCallback)
		return;

	ed_refreshCallback();
}

/*
 ==================
 ED_DrawEditBox
 ==================
*/
void ED_DrawEditBox (const odVec3 &color, const odVec3 corners[8]){

	if (ed_editorActive == EDITOR_NONE)
		return;

	R_DrawEditBox(color, corners);
}
